	
/**
 * ҳ�������ع��ﳵ
 */
$(document).ready(function(){
	
	$.ajax({
		type : "POST",
		dataType : "json",
		url : '/iwash/cart/shoppingCart/viewMyCart.do',
		success : function(data) {
			var html='';
			if(data && data.cartItems.length>0){
			    html = template("cart-detail", {cart:data});
			}else{
				html='购物车是空空的,不如去首页看看吧';
			}
			$('.cart-list').html(html);
		}
	});
	
});


function addGnum(domEle,flag){
	_domEle = $(domEle);
	_dataEle = _domEle.closest('tr');
	var num = _domEle.closest('td').find('input[name="mdyQuantity"]').val() || 1;
	var goodsId = _dataEle.attr('goodsid');
	var price = parseInt(_dataEle.attr('price'));
	var totalPrice=parseInt($('#totalPrice').html());
	if(flag==1){
		num++;
		totalPrice+=price;
	}else if(flag==-1){
		if(num>1){
			num--;
			totalPrice-=price;
		}else{
			return;
		}
	}
	_domEle.closest('td').find('input[name="mdyQuantity"]').val(num)
	
	$.ajax({
		type : "POST",
		url : '/iwash/cart/shoppingCart/changeGnumInCart.do',
		data : {"goodsId":goodsId,"quantity":1,"flag":flag},
		success : function(result) {
			_dataEle.attr('quantity',num);
			$('#totalPrice').html(totalPrice);
		}
	});
}

function changeGnum(domEle){
	_domEle = $(domEle);
	_dataEle = _domEle.closest('tr');
	var goodsId = _dataEle.attr('goodsid');
	var price = parseInt(_dataEle.attr('price'));
	var totalPrice=parseInt($('#totalPrice').html());

	var mdy_quantity = parseInt(_domEle.val());
	var old_quantity = parseInt(_dataEle.attr('quantity'));
	if(mdy_quantity != old_quantity){
		var flag = mdy_quantity-old_quantity<0?-1:1;
		var quantity = Math.abs(mdy_quantity-old_quantity);
		if(flag>0){
			totalPrice+=price*quantity;
		}else{
			totalPrice-=price*quantity;
		}
		$.ajax({
			type : "POST",
			dataType : "json",
			url : '/iwash/cart/shoppingCart/changeGnumInCart.do',
			data : {"goodsId":goodsId,"quantity":quantity,"flag":flag},
			success : function(result) {
				_dataEle.attr('quantity',mdy_quantity);
				$('#totalPrice').html(totalPrice);
			}
		});
	}
}

function removeItem(domEle){
	_domEle = $(domEle);
	var $tr = _domEle.closest('tr');
	var goodsIds=$tr.attr('goodsid');
	var quantity=parseInt($tr.attr('quantity'));
	var price=parseInt($tr.attr('price'));
	var totalPrice=parseInt($('#totalPrice').html());
	var $table=$(domEle).closest('table');
	totalPrice=totalPrice-(price*quantity);
	$.ajax({
		type : "POST",
		url : '/iwash/cart/shoppingCart/removeGoodsInCart.do',
		data : {"goodsIds":goodsIds},
		success : function(result) {
			if($table.find('tr').length==2){
				$table.remove();
				$('.cart-list').html('购物车是空空的,不如去首页看看吧');
			}else{
				$tr.remove();
				$('#totalPrice').html(totalPrice);
			}
		}
	});
}

function cleanCart(){
	var params='';
	$('.shop_tab').find('tr').each(function(){
		var goodsId=$(this).attr('goodsid');
		if(goodsId){
			if(params==''){
				params+='goodsIds='+goodsId
			}else{
				params+='&goodsIds='+goodsId
			}
		}
	});
	$.ajax({
		type : "POST",
		url : '/iwash/cart/shoppingCart/removeGoodsInCart.do?'+params,
		success : function(result) {
			$('.cart-list').html('购物车是空空的,不如去首页看看吧');
		}
	});
}
